package com.hooda.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangzp
 * @version 1.0, 2021/10/14
 */
@Component
public class String2MapConverter implements Converter<String, Map> {

    @Override
    public Map convert(String source) {
        if (!StringUtils.hasLength(source)) {
            return null;
        }

        return JSON.parseObject(source, new ParameterizedTypeImpl(new Type[]{String.class, Object.class}, HashMap.class, HashMap.class));
    }
}
